{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Linear Regression"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Linear Regression Params:[[4.21509616]\n",
      " [2.77011339]]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "# 生成示例数据\n",
    "np.random.seed(42)\n",
    "X=2*np.random.rand(100,1)\n",
    "y=4+3*X+np.random.randn(100,1)\n",
    "\n",
    "#初始化参数\n",
    "theta=np.random.randn(2,1)\n",
    "#添加偏置项\n",
    "X_b=np.c_[np.ones((100,1)),X]\n",
    "\n",
    "#超参数\n",
    "learning_rate=0.1\n",
    "n_iterations=1000\n",
    "m=100\n",
    "\n",
    "\n",
    "#梯度下降\n",
    "for iteration in range(n_iterations):\n",
    "    gradients=2/m*X_b.T.dot(X_b.dot(theta)-y)\n",
    "    theta=theta-learning_rate*gradients\n",
    "\n",
    "print(f\"Linear Regression Params:{theta}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Logistic Regression With Optimization"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Logistic Regression Params:[[582.05861739]\n",
      " [645.84768185]]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "\n",
    "# 生成示例数据\n",
    "np.random.seed(42)\n",
    "X = 2 * np.random.rand(100, 1)\n",
    "y = 4 + 3 * X + np.random.randn(100, 1)\n",
    "\n",
    "\n",
    "# 初始化参数\n",
    "theta = np.random.randn(2, 1)\n",
    "# 添加偏置项\n",
    "X_b = np.c_[np.ones((100, 1)), X]\n",
    "\n",
    "# 超参数\n",
    "learning_rate = 0.1\n",
    "n_iterations = 1000\n",
    "m = 100\n",
    "\n",
    "\n",
    "# Sigmoid 函数\n",
    "def sigmoid(z):\n",
    "    return 1 / (1 + np.exp(-z))\n",
    "\n",
    "\n",
    "# 梯度下降\n",
    "for iteration in range(n_iterations):\n",
    "    logits = X_b.dot(theta)\n",
    "    predictions = sigmoid(logits)\n",
    "    gradients = 1 / m * X_b.T.dot(predictions - y)\n",
    "    theta = theta - learning_rate * gradients\n",
    "\n",
    "\n",
    "print(f\"Logistic Regression Params:{theta}\")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "llm-learning",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.16"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
